package bb.lanxing.util.jsbridge;

import android.content.Context;
import android.webkit.WebView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class BridgeUtil {
    public static final String CALLBACK_ID_FORMAT = "JAVA_CB_%s";
    public static final String JAVASCRIPT_STR = "javascript:%s";
    public static final String JAVA_SCRIPT = "WebViewJavascriptBridge.js";
    public static final String JS_HANDLE_MESSAGE_FROM_JAVA = "javascript:WebViewJavascriptBridge._handleMessageFromNative('%s');";
    public static final String UNDERLINE_STR = "_";

    BridgeUtil() {
    }

    public static void webViewLoadJs(WebView view, String url) {
        view.loadUrl("javascript:" + (("var newscript = document.createElement(\"script\");newscript.src=\"" + url + "\";") + "document.scripts[0].parentNode.insertBefore(newscript,document.scripts[0]);"));
    }

    public static void webViewLoadLocalJs(WebView view, String path) {
        String assetFile2Str = assetFile2Str(view.getContext(), path);
        view.loadUrl("javascript:" + assetFile2Str);
    }


    public static String assetFile2Str(Context c, String urlStr) {
        InputStream inputStream;
        String readLine;
        InputStream inputStream2 = null;
        try {
            inputStream = c.getAssets().open(urlStr);
        } catch (Exception e) {
            e = e;
            inputStream = null;
        } catch (Throwable th) {
//            th = th;
            if (inputStream2 != null) {
            }
            throw th;
        }
        try {
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder sb = new StringBuilder();
                do {
                    readLine = bufferedReader.readLine();
                    if (readLine != null && !readLine.matches("^\\s*\\/\\/.*")) {
                        sb.append(readLine);
                        continue;
                    }
                } while (readLine != null);
                bufferedReader.close();
                inputStream.close();
                String sb2 = sb.toString();
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                }
                return sb2;
            } catch (Throwable th2) {
//                th = th2;
                inputStream2 = inputStream;
                if (inputStream2 != null) {
                    try {
                        inputStream2.close();
                    } catch (IOException e3) {
                        e3.printStackTrace();
                    }
                }
                throw th2;
            }
        } catch (Exception e4) {
//            e = e4;
            e4.printStackTrace();
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e5) {
                    e5.printStackTrace();
                }
            }
            return null;
        }
    }
}
